﻿#region

using System;
using System.Collections.Generic;
using System.Linq;
using HouseHoldBudget.Core.Data;
using HouseHoldBudget.Core.Entities.Budgets;
using HouseHoldBudget.Core.Entities.Incomes;

#endregion

namespace HouseHoldBudget.Services.Incomes
{
    public class FixedIncomesService : IFixedIncomesService
    {
        #region Readonly & Static Fields

        private readonly IRepository<FixedIncome> _fixedIncomeRepository;

        #endregion

        #region Constructors

        /// <summary>
        ///   Initializes a new instance of the <see cref="FixedIncomesService" /> class.
        /// </summary>
        /// <param name="fixedIncomeRepository"> The fixed income repository. </param>
        public FixedIncomesService(IRepository<FixedIncome> fixedIncomeRepository)
        {
            _fixedIncomeRepository = fixedIncomeRepository;
        }

        #endregion

        #region IFixedIncomesService Members

        /// <summary>
        ///   Deletes the fixed income.
        /// </summary>
        /// <param name="id"> The id. </param>
        public void DeleteFixedIncome(Guid id)
        {
            _fixedIncomeRepository.DeleteById(id);
        }

        /// <summary>
        /// Deletes the fixed income.
        /// </summary>
        /// <param name="fixedExpense">The fixed expense.</param>
        public void DeleteFixedIncome(FixedIncome fixedExpense)
        {
            _fixedIncomeRepository.Delete(fixedExpense);
        }

        /// <summary>
        /// Gets the fixed incomes by user id.
        /// </summary>
        /// <param name="userId">The user id.</param>
        /// <returns></returns>
        public List<FixedIncome> GetFixedIncomesByUserId(Guid userId)
        {
            var query = _fixedIncomeRepository.Table;
            query = query.Where(p => p.BudgetUserId == userId);
            var expenses = query.OrderByDescending(p => p.FixedIncomesName).ToList();
            return expenses;
        }

        /// <summary>
        ///   Gets the fixed income by id.
        /// </summary>
        /// <param name="budgetUserId"> The id. </param>
        /// <returns> </returns>
        public FixedIncome GetFixedIncomeById(Guid budgetUserId)
        {
            return _fixedIncomeRepository.GetById(budgetUserId);
        }

        /// <summary>
        ///   Inserts the fixed income.
        /// </summary>
        /// <param name="fixedExpense"> The fixed expense. </param>
        public void InsertFixedIncome(FixedIncome fixedExpense)
        {
            _fixedIncomeRepository.Insert(fixedExpense);
        }

        /// <summary>
        ///   Updates the fixed income.
        /// </summary>
        /// <param name="fixedExpense"> The expense. </param>
        public void UpdateFixedIncome(FixedIncome fixedExpense)
        {
            _fixedIncomeRepository.Update(fixedExpense);
        }

        #endregion
    }
}